<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" />
    <title>文档列表 - 光年(Light Year Admin)后台管理系统模板</title>
    <link rel="icon" href="favicon.ico" type="image/ico">
    <meta name="keywords" content="LightYear,光年,后台模板,后台管理系统,光年HTML模板">
    <meta name="description" content="LightYear是一个基于Bootstrap v3.3.7的后台管理系统的HTML模板。">
    <meta name="author" content="yinqi">
    <link href="/lib/css/bootstrap.min.css" rel="stylesheet">
    <link href="/lib/css/materialdesignicons.min.css" rel="stylesheet">
    <link href="/lib/css/style.min.css" rel="stylesheet">
    <link rel="stylesheet" href="/lib/layui/css/layui.css" media="all">
</head>

<body>
<div class="container-fluid p-t-15">

    <div class="row">
        <div class="col-lg-12">
            <div class="card">
                <div class="card-toolbar clearfix">
                    <form class="pull-right search-bar" method="get" action="#!" role="form">
                        <select class="btn btn-default dropdown-toggle" id="seo_keywords3" name="seo_keywords"  placeholder="请输入类别" >
                        <div class="input-group">
                            <input id="sname" type="text" class="form-control" placeholder="请输入产品名字">
                            <span class="input-group-btn">
                                <button id="search" class="btn btn-default" type="button">搜索</button>
                            </span>
                        </div>
                    </form>
                    <div class="toolbar-btn-action">
                        <a class="btn btn-primary m-r-5" id="add1" href="#!"><i class="mdi mdi-plus"></i> 新增</a>
                    </div>
                </div>
                <input type="hidden" th:value="${mid}" id="mid">
               <!-- <div class="card-body">

                    <div class="table-responsive">
                        <table class="table table-bordered">
                            <thead>
                            <tr>
                                <th>
                                    <label class="lyear-checkbox checkbox-primary">
                                        <input type="checkbox" id="check-all"><span></span>
                                    </label>
                                </th>
                                <th>ID</th>
                                <th>产品名称</th>
                                <th>产品单价</th>
                                <th>库存数量</th>
                                <th>状态</th>
                                <th>操作</th>
                            </tr>
                            </thead>
                            <tbody>
                            <tr>
                                <td>
                                    <label class="lyear-checkbox checkbox-primary">
                                        <input type="checkbox" name="ids[]" value="1"><span></span>
                                    </label>
                                </td>
                                <td>1</td>
                                <td>第01章 天涯思君不可忘</td>
                                <td>金庸</td>
                                <td>36</td>
                                <td><font class="text-success">正常</font></td>
                                <td>
                                    <div class="btn-group">
                                        <a class="btn btn-xs btn-default" href="#!" title="编辑" data-toggle="tooltip"><i class="mdi mdi-pencil"></i></a>
                                        <a class="btn btn-xs btn-default" href="#!" title="删除" data-toggle="tooltip"><i class="mdi mdi-window-close"></i></a>
                                    </div>
                                </td>
                            </tr>
                            </tbody>
                        </table>
                    </div>
                    <ul class="pagination">
                        <li class="disabled"><span>«</span></li>
                        <li class="active"><span>1</span></li>
                        <li><a href="#1">2</a></li>
                        <li><a href="#1">3</a></li>
                        <li><a href="#1">4</a></li>
                        <li><a href="#1">5</a></li>
                        <li><a href="#1">6</a></li>
                        <li><a href="#1">7</a></li>
                        <li><a href="#1">8</a></li>
                        <li class="disabled"><span>...</span></li>
                        <li><a href="#!">14452</a></li>
                        <li><a href="#!">14453</a></li>
                        <li><a href="#!">»</a></li>
                    </ul>

                </div>-->
            </div>
        </div>

    </div>

</div>
<table class="layui-hide" id="demo" lay-filter="test"></table>

<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">查看编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<div style=""></div>
<script src="/lib/layui/layui.js" charset="utf-8"></script>
<script type="text/html" id="titleTpl">
    {{#  if(d.state == '2'){ }}
    下架
    {{#  } else {d.state=='1' }}
    在售
    {{#  } }}
</script>
<script th:inline="none">
    //JavaScript代码区域
    layui.use(['element','jquery','table','layer'], function(){
        var element = layui.element;
        var layer = layui.layer;
        var table = layui.table;
        var $ = layui.jquery;
        //执行一个 table 实例
        table.render({
            elem: '#demo'
            ,height: 420
            ,url: '/gaolist' //数据接口 ajax
            //,where条件
            ,page: true //开启分页
            ,toolbar: true //开启工具栏，此处显示默认图标，可以自定义模板，详见文档
            ,totalRow: true //开启合计行
            ,cols: [[ //表头
                {title:'序列', type: 'numbers', width:120,fixed: 'left',sort: true}
                ,{field: 'name', title: '产品名称', width:120}
                ,{field: 'price', title: '产品单价', width:120,sort: true}
                ,{field: 'stock', title: '库存数量', width:120,sort: true}
                ,{field: 'typename', title: '产品类别', width:120}
                ,{field: 'state', title: '状态', width:160, templet: '#titleTpl'}
                ,{fixed: 'right',title:'操作', width: 165, align:'center', toolbar: '#barDemo'}
            ]]
        });
        //监听行工具事件
        table.on('tool(test)', function(obj){ //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data //获得当前行数据
                ,layEvent = obj.event; //获得 lay-event 对应的值
            if(layEvent === 'del'){
                layer.confirm('真的删除行么', function(index){
                    layer.close(index);
                    $.ajax({
                        url:"/deleteproduct",
                        type:'post',
                        data:{
                            id:data.id
                        },
                        datatyp:'json',
                        success:function (message) {
                            if (message.code==0){
                                //正确
                                obj.del(); //删除对应行（tr）的DOM结构
                                //向服务端发送删除指令
                                alert("删除成功！")
                            }else {
                                alert("删除失败！")
                            }
                        }
                    })

                });
            } else if(layEvent === 'edit'){
                layer.open({
                    type: 2,
                    content:'/product/update?id='+data.id,
                    where:{
                      id:data.id
                    },
                    area: ['600px', '400px'],
                    end:function (){
                        //重新加载table表格
                        table.reload('demo');
                    }
                })
            }
        });
        $("#search").on('click',function () {
            table.reload('demo',{
                url:'/gaolist'//数据接口ajax
                ,where:{//条件查询
                    name:$('#sname').val(),
                    id:$("#seo_keywords3").val()
                }
                ,page:{
                    curr:1
                }
            })
            return false;
        })
        $("#add1").on('click',function () {
            layer.open({
                type: 2,
                title:"添加产品",
                content:'/product/add?mid='+$('#mid').val(),
               /* shadeClose: true, //开启遮罩关闭*/
                area: ['600px', '400px'],
                end:function (){
                    //重新加载table表格
                    table.reload('demo');
                }
            })
        })
    });
</script>
<script th:inline="javascript">
    window.onload=function () {
        $.ajax({
            url: '/product/typename',//上传地址
            type: 'POST',
            cache: false,
            /* data: new FormData($('#form0')[0]),//表单数据*/
            processData: false,
            contentType: false,
            success: function (message) {
                if (message.code ==0) {
                    var type="<option value=''>类别</option>"
                    for (var i=0; i< message.data.length;i++){
                        type+="<option value='"
                            + message.data[i].id+
                            "'>\n" +
                            message.data[i].name
                            +"    </option>"

                    }
                    $("#seo_keywords3").append(type)
                }
            }
        })
    }
</script>
<script type="text/javascript" src="/lib/js/jquery.min.js"></script>
<script type="text/javascript" src="/lib/js/bootstrap.min.js"></script>
<script type="text/javascript" src="/lib/js/main.min.js"></script>
<script type="text/javascript">
    $(function(){
        $('.search-bar .dropdown-menu a').click(function() {
            var field = $(this).data('field') || '';
            $('#search-field').val(field);
            $('#search-btn').html($(this).text() + ' <span class="caret"></span>');
        });
    });
</script>
</body>
</html>